﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace PlanningGIS.MetadataEdit
{
    public partial class FrmFileList : Form
    {
        public FrmFileList()
        {
            InitializeComponent();
        }

        private void listBox1_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {

                string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
                try
                {
                    foreach (string str in files)
                    {
                        //if( System.IO.Path.GetExtension( str).ToLower()==".img")
                        listBox1.Items.Add(str);
                    }
                }
                catch (Exception ex)
                {

                }               
            }
        }

        private void listBox1_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            //openFileDialog1.Filter = "img(*.img)|*.img";
            openFileDialog1.Multiselect = true;

            if (openFileDialog1.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            string[] strFiles = openFileDialog1.FileNames;
            for (int i = 0; i < strFiles.Length; i++)
            {
                if (!listBox1.Items.Contains(strFiles[i]))
                {
                    listBox1.Items.Add(strFiles[i]);
                }
            }         
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedItems.Count > 0)
            {
                while (listBox1.SelectedItems.Count > 0)
                {
                    listBox1.Items.Remove(listBox1.SelectedItems[0]);
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (listBox1.Items.Count == 0) return;
            int count = 0;
            double dblLong, dblLat;
             char rowNoMillion;  string strColNoMillion;  string strRowNo;  string strColNo;
            foreach (string str in listBox1.Items)
            {
                if (System.IO.File.Exists(str))
                {
                    try
                    {
                        string strPath = System.IO.Path.GetDirectoryName(str);
                        string fileName = System.IO.Path.GetFileNameWithoutExtension(str).Trim();
                        string strExtension = System.IO.Path.GetExtension(str);
                        string strMapNo = fileName.ToUpper();
                        if (strMapNo.IndexOf(".") > 0) strMapNo = strMapNo.Substring(0,strMapNo.IndexOf("."));
                        switch (strMapNo[3])
                        {
                            case 'H':
                                if (!MapNoUtility.CheckTfbh5000(strMapNo, out rowNoMillion, out strColNoMillion, out strRowNo, out strColNo, out dblLong, out dblLat))
                                    continue;

                                break;
                            case 'G':
                                if (!MapNoUtility.CheckTfbh10000(strMapNo, out rowNoMillion, out strColNoMillion, out strRowNo, out strColNo, out dblLong, out dblLat))
                                    continue;
                                break;
                            case 'E':
                                if (!MapNoUtility.CheckTfbh50000(strMapNo, out rowNoMillion, out strColNoMillion, out strRowNo, out strColNo, out dblLong, out dblLat))
                                    continue;
                                break;
                            default:
                                {
                                    continue;
                                }
                        }

                        if (dblLong >= 115.5 & dblLong < 118.5)
                        {
                            //System.IO.File.Copy(str, strPath + "\\P117-" + fileName + strExtension, true);
                            //System.IO.File.Delete(str);
                            System.IO.File.Move(str, strPath + "\\P117-" + fileName + strExtension);
                            count++;
                        }
                        else if (dblLong >= 118.5 & dblLong < 121.5)
                        {
                            //System.IO.File.Copy(str, strPath + "\\P120-" + fileName + strExtension, true);
                            //System.IO.File.Delete(str);

                            System.IO.File.Move(str, strPath + "\\P120-" + fileName + strExtension);
                            count++;
                        }
                        else if (dblLong >= 121.5 & dblLong < 124.5)
                        {
                            //System.IO.File.Copy(str, strPath + "\\P123-" + fileName + strExtension, true);
                            //System.IO.File.Delete(str);
                            System.IO.File.Move(str, strPath + "\\P123-" + fileName + strExtension);
                            count++;
                        }
                    }
                    catch (System.Exception)
                    {
                    }
                }
            }
            MessageBox.Show("重命名了"+count+"个文件");
        }

        private void btnExp_Click(object sender, EventArgs e)
        {
            if (listBox1.Items.Count == 0) return;
            SaveFileDialog sfDlg = new SaveFileDialog();
            sfDlg.Filter = "txt文件(*.txt)|*.txt";
            sfDlg.RestoreDirectory = true;
            if (sfDlg.ShowDialog() != DialogResult.OK) return;
            string strFile = sfDlg.FileName;

            StreamWriter s = new StreamWriter(strFile, false, System.Text.Encoding.Default);
            try
            {
                foreach (string str in listBox1.Items)
                {
                    s.WriteLine(str);
                }

                MessageBox.Show("导出完成!","提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception)
            {
            }
            finally
            {
                s.Close();
                s.Dispose();
            }
        }
    }
}
